/*
    This source file is part of GeNa project.
    Copyright © GeNa project 2008
    For the latest info, see http://code.google.com/p/genaproject/ or  http://lumpyproject.forums-rpg.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */

#include "gl_window.h"


#ifndef WX_PCH_H_INCLUDED
#include "wx_pch.h"
#endif

int attribList[] = {
    WX_GL_RGBA,
    WX_GL_DOUBLEBUFFER,
    WX_GL_DEPTH_SIZE, 16,
    WX_GL_STENCIL_SIZE, 8,
    0 };

BEGIN_EVENT_TABLE(GLWindow, wxGLCanvas)
END_EVENT_TABLE()

GLWindow* GLWindow::firstWnd = NULL;
wxGLContext* GLWindow::context = NULL;
wxFrame* GLWindow::baseFrame = NULL;

void GLWindow::initGLWindow()
{
    baseFrame = new wxFrame(NULL, wxID_ANY, _("OpenGl main frame"));
    wxGLCanvas* canvas = new wxGLCanvas(baseFrame, wxID_ANY, wxDefaultPosition, wxDefaultSize);
    baseFrame->Show();
    context = canvas->GetContext();
    baseFrame->Hide();
}

void GLWindow::deinitGLWindow()
{
    baseFrame->Close();
}

GLWindow::GLWindow(wxWindow* parent, wxPoint position, wxSize size) : wxGLCanvas(parent, context, -1, position, size, 0, _("GLCanvas"), attribList)
{
    nextWnd = firstWnd;
    firstWnd = this;
    isInitialized = false;
    time = 0;

    SetCurrent();
}

GLWindow::~GLWindow()
{
    if( firstWnd == this)
        firstWnd = nextWnd;
    else
    {
        GLWindow* wnd = firstWnd;
        while( wnd->nextWnd != this)
            wnd = wnd->nextWnd;
        wnd->nextWnd = nextWnd;
    }
}

void GLWindow::processRenderTimer(int interval)
{
    GLWindow* wnd = firstWnd;

    while(wnd)
    {
        wnd->time += interval;
        if( !wnd->isInitialized)
        {
            wnd->SetCurrent();
            wnd->init();
            wnd->isInitialized = true;
        }

        wnd->SetCurrent();
        wnd->update( interval);
        wnd = wnd->nextWnd;
    }
}



